﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class LastTopics : BaseUserControl
    {
        private Int32 UserRightLevel { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            LastTopicsObjectDataSource.SelectParameters["AnonymousID"].DefaultValue = (Session["AnonymousID"] == null) ? String.Empty : Session["AnonymousID"].ToString();
            LastTopicsObjectDataSource.SelectParameters["requesterTimeZone"].DefaultValue = UserProfile.GetUserProfile().TimeZone;

            // Les utilisateurs anonymes n'ont pas accès à la fonctionnalité message non-lu, ni à la création de sujet
            if (Session["AnonymousID"] == null)
            {
                NewTopicHyperLink.Visible = true;
                NewTopicHyperLink.NavigateUrl = String.Format("~/{0}/topicdetail/0/0/", ThisMaster.Requester.Culture);
                UnreadHyperLink.Visible = true;
                UnreadHyperLink.NavigateUrl = String.Format("~/{0}/topic/unread/", ThisMaster.Requester.Culture);
            }
            else
            {
                NewTopicHyperLink.Visible = false;
                UnreadHyperLink.Visible = false;
            }
        }

        protected void LastTopicsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ListViewItem item = e.Item;
            HyperLink forumPostHyperLink, lastPostHyperLink;

            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                // ForumPostHyperLink.NavigateUrl contient ForumTopicId
                forumPostHyperLink = (HyperLink)item.FindControl("ForumPostHyperLink");

                if (forumPostHyperLink != null)
                {
                    forumPostHyperLink.NavigateUrl = String.Format("~/{0}/forumpost/{1}/?topicpagenumber={2}",
                        ThisMaster.Requester.Culture,
                        forumPostHyperLink.NavigateUrl,
                        ((Page.RouteData.Values["topicPageNumber"] != null) ? Page.RouteData.Values["topicPageNumber"].ToString() : String.Empty));
                }

                lastPostHyperLink = (HyperLink)((ListViewItem)e.Item).FindControl("LastPostHyperLink");
                if (lastPostHyperLink != null)
                    lastPostHyperLink.NavigateUrl = String.Format("~/{0}/forumpost/showpost/{1}/", ThisMaster.Requester.Culture, Int32.Parse(lastPostHyperLink.NavigateUrl));
            }
        }
    }
}